package com.ms.action.basic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eddy.util.AjaxOut;
import com.ms.dao.entity.basic.UserEty;
import com.ms.dao.entity.basic.UserShortcutsEty;
import com.ms.dao.mapper.basic.UserShortcutsDao;

@Controller
@RequestMapping("/basic/UserSettingAction/")
public class UserSettingAction {
	
	@Autowired
	private UserShortcutsDao userShortcutsDao;
	
	@RequestMapping("search.action")
	public void search(HttpServletRequest request, HttpServletResponse response) {
		try {
			UserEty userEty = (UserEty) request.getSession().getAttribute("UserEty");
			UserShortcutsEty ety = new UserShortcutsEty();
			ety.setUserId(userEty.getId());
			List list = userShortcutsDao.selectByEntity(ety);
			AjaxOut.responseJSonGrid(response, list, list.size());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("saveUserShortcut.action")
	public void saveUserShortcut(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String resourceList = request.getParameter("resourceList");
			String shortcutType = request.getParameter("shortcutType");
			UserEty userEty = (UserEty) request.getSession().getAttribute("UserEty");
			
			JSONArray arr = JSONArray.fromObject(resourceList);
			for(int i = 0; i < arr.size(); i++) {
				UserShortcutsEty ety = new UserShortcutsEty();
				ety.setShortcutType(shortcutType);
				ety.setResourceId(Integer.parseInt(arr.get(i).toString()));
				ety.setUserId(userEty.getId());
				
				List list = userShortcutsDao.selectByEntity(ety);
				if(list.size() == 0) {
					userShortcutsDao.insert(ety);
				}
			}
			
			obj.put("result","success");
		}
		catch (Exception e) {
			e.printStackTrace();
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
}
